package com.frontop.system.modules.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frontop.common.domain.UserJurisdiction;
import com.frontop.common.util.JwtUtil;
import com.frontop.common.util.ResultJsonUtil;
import com.frontop.system.modules.sys.model.SysRole;
import com.frontop.system.modules.sys.model.SysRoleMenu;
import com.frontop.system.modules.sys.service.SysRoleMenuService;
import com.frontop.system.modules.sys.service.SysRoleService;
import com.frontop.system.modules.sys.vo.SysRoleVO;
import com.frontop.system.modules.user.service.TUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author YangJia
 * @since 2022-01-18
 */
@RestController
@RequestMapping("/sys-role")
public class SysRoleController {
    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysRoleMenuService sysRoleMenuService;

    @Autowired
    TUserService tUserService;


    @ApiOperation(value = "角色列表")
    @PostMapping("/selectRolePage")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public ResultJsonUtil<Object> selectRolePage(@RequestBody(required = false) SysRoleVO vo,
                                                          Authentication authentication,
                                                          HttpServletRequest request) {
        //获取当前登录用户基本权限信息
        UserJurisdiction userJurisdiction = JwtUtil.getUserJurisdiction(authentication);
        //只能查看子组织和自己组织的用户
        vo.setLowerOrganizationIds(userJurisdiction.getLowerOrganizationIds());

        Page<SysRole> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        page = sysRoleService.selectRolePage(page,vo);
        return new ResultJsonUtil().success(page);
    }

    @ApiOperation(value = "添加修改角色的信息")
    @PostMapping("/addOrUpRole")
    @PreAuthorize("hasAuthority('sys:role:edit')")
    public ResultJsonUtil<Object> addOrUpRole(@RequestBody(required = false) SysRole sysRole, HttpServletRequest request) {
        if(sysRole.getId() != null){
            Map<String,Object> map = new HashMap<>();
            map.put("role_id",sysRole.getId());
            sysRoleMenuService.removeByMap(map);
        }
        sysRoleService.saveOrUpdate(sysRole);
        for (Integer menuId:sysRole.getMenuList()){
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(sysRole.getId());
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenuService.save(sysRoleMenu);
        }
        return new ResultJsonUtil().success("请求成功");
    }

    @ApiOperation(value = "获取角色拥有菜单权限")
    @GetMapping("/getMenuIdsByRole/{roleId}")
    public ResultJsonUtil<Object> getMenuIdsByRole(@PathVariable("roleId") Integer roleId, HttpServletRequest request) {
        List<Integer> menuIds = sysRoleService.getMenuIdsByRole(roleId);
        return new ResultJsonUtil().success(menuIds);
    }



    @ApiOperation(value = "删除角色")
    @GetMapping("/removeRoleById/{roleId}")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public ResultJsonUtil<Object> removeRoleById(@PathVariable("roleId") Integer roleId, HttpServletRequest request) {
        //获取当前角色拥有的菜单中间表
        List<Integer> menuRoleIds = sysRoleMenuService.getSysRoleMenuIdsByRole(roleId);
        //删除角色相关的菜单中间表
        sysRoleMenuService.removeByIds(menuRoleIds);
        //删除角色
        sysRoleService.removeById(roleId);
        return new ResultJsonUtil().success("删除成功");
    }

    @ApiOperation(value = "根据组织获取角色")
    @GetMapping("/getRoleListByOrgId/{orgId}")
    public ResultJsonUtil<Object> getRoleListByOrgId(@PathVariable("orgId") Integer orgId, HttpServletRequest request) {
        Map<String,Object> map = new HashMap<>();
        map.put("organization_id",orgId);
        List<SysRole> sysRoleList  = sysRoleService.listByMap(map);
        return new ResultJsonUtil().success(sysRoleList);
    }
}